package com.my.bookstore.web;

import java.util.List;

/**
 * Created by YJH on 2017/7/17 17:13.
 */
public class Page<T> {

    /**
     * 当前第几页
     */
    private int pageNo;

    /**
     * 当前页的List
     */
    private List<T> list;

    /**
     * 煤业显示多少条记录
     */
    private int pageSize = 3;

    /**
     * 共有多少条记录
     */
    private long totalItemNum;

    public Page(int pageNo) {
        this.pageNo = pageNo;
    }

    //需要校验pageNo
    public int getPageNo() {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageNo > getTotalPageNum()) {
            pageNo = getTotalPageNum();
        }
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setTotalItemNum(long totalItemNum) {
        this.totalItemNum = totalItemNum;
    }

    /**
     * 获取总页数
     *
     * @return 总页数
     */
    public int getTotalPageNum() {
        int totalItemNum = ((int) (this.totalItemNum)) / pageSize;
        if (this.totalItemNum % pageSize != 0) {
            totalItemNum++;
        }
        return totalItemNum;
    }

    /**
     * 是否有下一页
     *
     * @return boolean
     */
    public boolean isHasNext() {
        return getPageNo() < getTotalPageNum();
    }

    /**
     * 是否有上一页
     *
     * @return boolean
     */
    public boolean isHasPrev() {
        return getPageNo() > 1;
    }

    /**
     * 得到上一页
     *
     * @return int
     */
    public int getPrevPage() {
        if (isHasPrev()) {
            return getPageNo() - 1;
        }
        return getPageNo();
    }

    /**
     * 获取下一页
     *
     * @return int
     */
    public int getNextPage() {
        if (isHasNext()) {
            return getPageNo() + 1;
        }
        return getPageNo();
    }
}
